#!/usr/bin/node

const fs = require('fs'),
    http = require('http'),
    url = require('url');

const movies = require('./data.js');
let movielist = movies.filter((item) => item.movieRange !== "暂无评分")
movielist.sort(function (a, b) { return Number(b.movieRange) - Number(a.movieRange) });

function getPage() {
    let html = fs.readFileSync('./index.html');
    // html = html.toString().replace('/getlist', 'http://localhost:8080/getlist');
    return html;
}
http.createServer((req, res) => {
    let addr = url.parse(req.url);

    if (addr.pathname == '/') {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/html');
        res.end(getPage());
        return;
    }
    if (addr.pathname == '/getlist') {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/json');
        res.end(JSON.stringify(movielist));
        return;
    }

}).listen(8080);
